package com.account.service;

import com.account.dao.AccountDao;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.seata.api.AccountService;
import com.seata.entity.Account;
import io.seata.core.context.RootContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;

@Slf4j
@DubboService
public class AccountServiceImpl implements AccountService {
    @Resource
    AccountDao accountDao;

    @Override
    public void debit(String userId, int money) {
        log.info("从用户账户中借出。。。。{}，全局事务id: {}", money, RootContext.getXID());
        Account account = accountDao.selectOne(new LambdaQueryWrapper<Account>()
                .eq(Account::getUserId, userId));
        if (account.getMoney() == 0) {
            throw new RuntimeException("余额不足........");
        }
        account.setMoney(account.getMoney() - money);
        // 扣减金额
        accountDao.updateById(account);
    }

}